package com.xu.drools.config;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.netty.server.servlet.NettyServletResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author:whh
 * @date: 2025-08-17 14:27
 * <p></p>
 */
public class CrosInterceptor extends AbstractPhaseInterceptor<Message> {

    public CrosInterceptor() {
        super(Phase.SEND);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        Exchange exchange = message.getExchange();
        Message outMessage = exchange.getOutMessage();

        NettyServletResponse response = (NettyServletResponse) outMessage.get("HTTP.RESPONSE");

        if(response == null)return;


        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers","Content-Type, Authorization");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Max-Age","86400");
    }
}
